package com.zjitc.controller;

import com.zjitc.common.ServerResponse;
import com.zjitc.model.User;
import com.zjitc.service.UserService;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/27 16:50
 * @description:
 */
@Controller
@RequestMapping("/user")
public class UserController {

  @Autowired
  private UserService userService;

  @RequestMapping("/login")
  @ResponseBody
  public ServerResponse login(
      @RequestParam(name = "username") String username,
      @RequestParam(name = "password") String password,
      HttpSession session) {
    ServerResponse<User> response = userService.login(username, password);
    if (response.success()) {
      session.setAttribute("user", response.getData());
    }

    return response;
  }

  @RequestMapping("/register")
  @ResponseBody
  public ServerResponse register(User user) {
    ServerResponse<User> response = userService.register(user);
    return response;
  }

  @RequestMapping("/checkValid")
  @ResponseBody
  public ServerResponse checkValid(
      @RequestParam("str") String str) {
    ServerResponse<User> response = userService.checkValid(str);
    return response;
  }

  @RequestMapping("getUserInfo")
  @ResponseBody
  public ServerResponse getUserInfo(HttpSession session){
    User currentUser = (User) session.getAttribute("user");
    ServerResponse<User> response = userService.getUserInfo(currentUser.getUsername());
    if (currentUser == null){
      return ServerResponse.createError("用户未登录，需要先登录");
    }
    return response;
  }

  @RequestMapping("forgetGetQuestion")
  @ResponseBody
  public ServerResponse forgetGetQuestion(String username){
    ServerResponse<User> response = userService.forgetGetQuestion(username);
    return response;
  }

  @RequestMapping("forgetCheckAnswer")
  @ResponseBody
  public ServerResponse forgetCheckAnswer(String username,String question, String answer){
    ServerResponse<User> response = userService.checkQuestionAndAnswer(username,question,answer);
    return response;
  }

  @RequestMapping("forgetResetPassword")
  @ResponseBody
  public ServerResponse forgetResetPassword(String username,String passwordNew,String forgetToken){
    ServerResponse<User> response = userService.forgetResetPassword(username,passwordNew,forgetToken);
    return response;
  }


  @RequestMapping("resetPassword")
  @ResponseBody
  public ServerResponse resetPassword(HttpSession session, String passwordOld, String passwordNew){
    User user = (User) session.getAttribute("user");
    ServerResponse<User> response = userService.resetPassword(passwordOld,passwordNew,user);
    if(user == null){
      return ServerResponse.createError("用户未登录");
    }
    return response;
  }

  @RequestMapping("updateInformation")
  @ResponseBody
  public ServerResponse updateInformation(HttpSession session, String email, String phone,String question,String answer){
    User user = (User) session.getAttribute("user");
    ServerResponse<User> response = userService.updateInformation(email,phone,question,answer,user);
    if(user == null){
      return ServerResponse.createError("用户未登录");
    }
    return response;
  }

  @RequestMapping("getInformation")
  @ResponseBody
  public ServerResponse getInformation(HttpSession session){
    User user = (User) session.getAttribute("user");
    ServerResponse<User> response = userService.getInformation(user.getUsername());
    if(user == null){
      return ServerResponse.createError("用户未登录");
    }
    return response;
  }

  @RequestMapping("logout")
  @ResponseBody
  public ServerResponse logout(HttpSession session){
    User user = (User) session.getAttribute("user");
    session.removeAttribute("user");
    ServerResponse<User> response = userService.logout(user);
    return response;
  }
}
